package cn.ecut.servlet;

import cn.ecut.pojo.Person;

import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;

/**
 * @Author ruanxin
 */
public class SessionDemo01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //解决中文乱码问题
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");

        //获取Session
        HttpSession session = req.getSession();

        //在Session中存入东西
        session.setAttribute("name",new Person("阮欣",20));
        //获取Session中的ID
        String id = session.getId();
        System.out.println(id);
        //判断Session是不是新创建
        if(session.isNew()){
            resp.getWriter().write("新Session创建成功，Id为："+id);
        }else{
            resp.getWriter().write("已存在旧的Session，Id为："+id);
        }

        //Session在创建的时候做了些什么事
//        Cooike cooike=new Cookie("JSESSIONID",sessionId);
//        resp.addCookie(cooike);


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
